import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { RoomListComponent } from './room-list/room-list.component';
import { NzMessageService, NzModalRef,NzModalService } from 'ng-zorro-antd';
import { data } from 'jquery';

@Component({
  selector: 'bind-room',
  templateUrl: './bind-room.component.html',
  styles: []
})

export class BindRoomComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf: SFComponent;
  @Input()type: 0 | 1;
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
    console.log(this.param);
   // this.param
    // this.getDeviceInfo(value.id).then(res => {
    //   console.log(res);
    //   this.param = res.data.deviceHostRke;
      
    // })

  };
  houseList:any[];
  param: any;
  showForm = false;
  schema: SFSchema = {
    properties: {
      areaParentAllName:{
        type:'string',
        title:'关联区域',
        readOnly: true,
      },
      // qrCode: {
      //   type: 'number',
      //   title: '具体位置',
      //   enum: [
      //     {label: '负一层', value: 1},
      //     {label: '负二层', value: 2},
      //     {label: '负三层', value: 3}
      //   ],
      //   ui: {
      //     widget: 'select',
      //     hidden: true
      //   }
      // },
      deviceDescribe: {
        type: 'string',
        title: '设备描述',
      },
    },
  }

  selectRoom(){
    const modal = this.modal.create({
      nzTitle: '选择区域',
      nzContent: RoomListComponent,
      nzWidth: 800,
      nzComponentParams: {
        
      },
      nzOnOk: (e) => {
        if(e.selectData == undefined){
          return
        }
        this.houseList = e.selectData
        let areaName=""
        for(let item of e.selectData){
          areaName += item.areaParentAllName + ";"
        }
        this.param.areaParentAllName = areaName
        // this.param.areaParentAllName = e.selectData.areaParentAllName;
        // this.param.houseId = e.selectData.id;
        //根据选择的楼栋单元查询此楼栋下是否有具体位置，如果有，则显示具体位置下拉框，否则不显示
        // this.schema.properties["qrCode"].ui["hidden"] = false
        this.sf.refreshSchema();
      },
      nzFooter: [
        {
          label:'取消',
          onClick:() => modal.destroy() 
        },
        {
          label:'确定',
          type:'primary',
          onClick:()=>{
            modal.triggerOk()
          }
        }
      ]
    })
  }

  //根据选择的小区楼栋查询下面的门禁
  selectDoorByArea(id){
    this.http.post("/core/area/host/queryDoorList",{areaId:id}).subscribe((res:any)=>{
      console.log(res)
      if(res.status == 200){
        const statusProperty = this.sf.getProperty('/deviceDescribe')!;
        statusProperty.schema.enum = res.data.map(item => {
          return {label:item.doorDescribe,value:item.id,sipId:item.sipId};
        })
        statusProperty.widget.reset('');
      }
    })
  }

  formSubmit(data) {
    if(this.houseList.length <= 0){
      this.msg.warning('未选择房屋!')
      return;
    }
   const list = this.houseList.map(item=>{
     return {
      deviceId:data.id,
      areaId: item.id,
      areaParentAllName:item.areaParentAllName,
      deviceDescribe:data.deviceDescribe
     }
   })
    const url = '/core/host/hostBinding';
    const method = 'POST';
      this.http.request<any>(method, url, { body:list }).subscribe(res => {
        if (res.status === '200') {
          this.msg.success('操作成功');
          this.modalRef.triggerOk();
        } else {
          this.msg.error(res.message)
        }
      })
  }
  constructor(
    private api: ApiService,
    private http: HttpClient,
    private msg: NzMessageService,
    private modalRef: NzModalRef,
    private modal: NzModalService,
  ) { }

  ngOnInit() {
  }

}
